﻿using ModuleA.Event;
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModuleA.ViewModels
{
    public class ViewCViewModel : IDialogAware
    {
        private readonly IEventAggregator aggregator;
        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }

        public ViewCViewModel(IEventAggregator aggregator)
        {
            CancelCommand = new DelegateCommand(Cancel);
            SaveCommand = new DelegateCommand(Save);
            this.aggregator = aggregator;
        }

        private void Save()
        {
            OnDialogClosed();
        }

        private void Cancel()
        {
            //发布消息MessageEvent内容为字符串Hello-message
            aggregator.GetEvent<MessageEvent>().Publish("Hello-message");
       //     RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }

        public string Title { get; set; }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            DialogParameters keys = new DialogParameters();
            keys.Add("Value", "ViewCHello2");
            //把变量名为Value，值为ViewCHello2的数据传递给主窗口，主窗口可以根据传递的数据进行相关事务的处理
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keys));
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            //接收主窗口传递过来的变量名为Title的值，赋值给子窗口的变量Title,但是为啥这个Title真成了标题，有待研究
            Title = parameters.GetValue<string>("Title");
        }
    }
}
